package cn.edu.sdjzu.xg11.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.TreeSet;

import org.springframework.stereotype.Repository;

import cn.edu.sdjzu.xg11.domain.ProjectCategory;
@Repository
public final class ProjectCategoryDao {
	//本类的一个对象引用，保存自身对象。
	private static ProjectCategoryDao projectCategoryDao = null;	
	//私有的构造方法，防止其它类创建它的对象
	private ProjectCategoryDao(){}
	//静态方法，返回本类的惟一对象
	public synchronized static ProjectCategoryDao getInstance() {
		return projectCategoryDao == null ? new ProjectCategoryDao()
				: projectCategoryDao;
	}

	private static Collection<ProjectCategory> projectCategorys;
	static {
		projectCategorys = new TreeSet<ProjectCategory>();
		ProjectCategory design = new ProjectCategory(1, "设计", "01", "");
		projectCategorys.add(design);
		projectCategorys.add(new ProjectCategory(2, "论文", "02", ""));

	}
	
	public Collection<ProjectCategory> getProjectCategories() {
		return ProjectCategoryDao.projectCategorys;
	}

	public ProjectCategory getProjectCategory(Integer id) {

		for (ProjectCategory projectCategory : projectCategorys) {
			if (id.equals(projectCategory.getId())) {
				return projectCategory;
			}
		}

		return null;
	}

	public boolean updateProjectCategory(ProjectCategory projectCategory) {
		projectCategorys.remove(projectCategory);
		return projectCategorys.add(projectCategory);
	}

	public boolean addProjectCategory(ProjectCategory projectCategory) {
		return projectCategorys.add(projectCategory);
	}

	public boolean deleteProjectCategory(Integer id) {
		ProjectCategory projectCategory = this.getProjectCategory(id);
		return this.deleteProjectCategory(projectCategory);
	}

	public boolean deleteProjectCategory(ProjectCategory projectCategory) {
		return projectCategorys.remove(projectCategory);
	}

	public static void main(String[] args) {
		ProjectCategoryDao dao = new ProjectCategoryDao();
		Collection<ProjectCategory> projectCategorys = dao
				.getProjectCategories();
		display(projectCategorys);
	}

	private static void display(Collection<ProjectCategory> projectCategorys) {
		for (ProjectCategory projectCategory : projectCategorys) {
			System.out.println(projectCategory);
		}
	}

}
